package fr.km.client.view.impl;

import java.util.ArrayList;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.Hyperlink;
import com.google.gwt.user.client.ui.InlineHyperlink;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

import fr.km.client.KMService;
import fr.km.client.KMServiceAsync;
import fr.km.client.model.Article;
import fr.km.client.model.ModelManager;
import fr.km.client.presenter.impl.ArticleEditorPresenter;

public class MainView extends Composite {
	/**
	 * Create a remote service proxy to talk to the server-side Greeting service.
	 */
	private final KMServiceAsync kmService = GWT
			.create(KMService.class);
	
	private VerticalPanel articlesFound;
	private SearchOverviewView searchOverview;
	
	public MainView() {
		final TextBox searchField = new TextBox();
		searchField.setStyleName("search-field");
		
		Button searchButton = new Button("Search");
		searchButton.setStyleName("search-button");
		
		articlesFound = new VerticalPanel();
		articlesFound.setWidth("100%");
		
		searchButton.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				kmService.findArticles(searchField.getText(), new AsyncCallback<ArrayList<Article>>() {

					@Override
					public void onFailure(Throwable caught) {
						searchField.setText("Failed");
						
					}

					@Override
					public void onSuccess(ArrayList<Article> result) {
						setArticlesFound(result);
					}
					
				});
			}
		}
		);
		
		InlineHyperlink createButton = new InlineHyperlink("Create article", "create_article");
		createButton.setStyleName("create-article");
		createButton.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				ArticleEditorPresenter articleEditorPresenter = new ArticleEditorPresenter(new Article(), new ModelManager());
				ArticleEditorView articleEditorView = new ArticleEditorView();
				articleEditorPresenter.setView(articleEditorView);
				articleEditorView.setPresenter(articleEditorPresenter);
				articleEditorPresenter.displayArticle();
			}
		});
		
		searchOverview = new SearchOverviewView();
		
		FlowPanel searchArea = new FlowPanel();
		searchArea.setStyleName("search-area");
		
		
		searchArea.add(searchField);
		searchArea.add(searchButton);
		searchArea.add(createButton);
		
		VerticalPanel area = new VerticalPanel();
		area.setWidth("100%");
		
		area.add(searchArea);
		area.add(searchOverview);
		area.add(articlesFound);
		
		// style .gwt-DecoratorPanel .middleCenter
		DecoratorPanel decoratedArea = new DecoratorPanel();
		
		decoratedArea.add(area);
		
		initWidget(decoratedArea);
	}
	
	private void setArticlesFound(ArrayList<Article> articles) {
		searchOverview.setDetails(1, 10, 142);
		articlesFound.clear();
		for(Article a : articles) {
			articlesFound.add(new ArticleFoundView(a));
		}
	}
}
